package com.hksj.drcs.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hksj.common.core.model.Rest;
import com.hksj.drcs.dto.create.CreateScheduleTaskDTO;
import com.hksj.drcs.dto.delete.DeleteScheduleTaskDTO;
import com.hksj.drcs.dto.update.UpdateScheduleTaskDTO;
import com.hksj.drcs.dto.update.UpdateScheduleTaskStatusDTO;
import com.hksj.drcs.query.RemoteControlDeviceQuery;
import com.hksj.drcs.query.ScheduleTaskQuery;
import com.hksj.drcs.service.ScheduleTaskService;
import com.hksj.drcs.vo.RemoteControlDeviceVO;
import com.hksj.drcs.vo.ScheduleTaskBaseInfoVO;
import com.hksj.drcs.vo.ScheduleTaskVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zengke
 * @version 1.0
 * @see ScheduleTaskController
 * @since 2025/05/22 10:33
 */

@Api(tags = "设备远程控制-定时任务管理")
@RequestMapping("/api/scheduleTask")
@RestController
public class ScheduleTaskController {

    @Autowired
    @Qualifier(value = "scheduleTaskServiceImpl")
    private ScheduleTaskService scheduleTaskService;

    @ApiOperation(value = "创建定时任务")
    @PostMapping(value = "/createScheduleTask")
    public Rest<String> createScheduleTask(@RequestBody CreateScheduleTaskDTO dto) {
        String result = scheduleTaskService.createScheduleTask(dto);
        return Rest.ok(result);
    }

    @ApiOperation(value = "更新定时任务")
    @PutMapping(value = "/updateScheduleTask")
    public Rest<String> updateScheduleTask(@RequestBody UpdateScheduleTaskDTO dto) {
        String result = scheduleTaskService.updateScheduleTask(dto);
        return Rest.ok(result);
    }

    @ApiOperation(value = "更新定时任务状态")
    @PutMapping(value = "/updateScheduleTaskStatus")
    public Rest<String> updateScheduleTaskStatus(@RequestBody UpdateScheduleTaskStatusDTO dto) {
        String result = scheduleTaskService.updateScheduleTaskStatus(dto);
        return Rest.ok(result);
    }

    @ApiOperation(value = "删除定时任务")
    @DeleteMapping(value = "/deleteScheduleTask")
    public Rest<String> deleteScheduleTask(@RequestBody DeleteScheduleTaskDTO dto) {
        String result = scheduleTaskService.deleteScheduleTask(dto);
        return Rest.ok(result);
    }

    @ApiOperation(value = "动态查询定时任务列表",response = ScheduleTaskVO.class)
    @PostMapping(value = "/queryScheduleTaskList")
    public Rest queryScheduleTaskList(@RequestBody ScheduleTaskQuery query) {

        //判断是否进行分页查询
        if (query.getDisAblePage()) {

            //不使用分页查询、动态查询定时任务列表
            List<ScheduleTaskVO> scheduleTaskList = scheduleTaskService.queryScheduleTaskList(query);

            //返回查询结果
            return Rest.ok(scheduleTaskList);
        } else {

            //使用分页查询
            IPage<ScheduleTaskVO> page =  scheduleTaskService.queryScheduleTaskListByPaging(query);

            return Rest.ok(page);
        }

    }

    @ApiOperation(value = "根据定时任务id查询定时任务基础信息")
    @GetMapping(value = "/queryScheduleTaskBaseInfo/{id}")
    public Rest<ScheduleTaskBaseInfoVO> queryScheduleTaskBaseInfo(@PathVariable(value = "id") String id){
        ScheduleTaskBaseInfoVO scheduleTaskBaseInfo = scheduleTaskService.queryScheduleTaskBaseInfo(id);
        return Rest.ok(scheduleTaskBaseInfo);
    }

    @ApiOperation(value = "查询远程集中控制设备树")
    @PostMapping(value = "/queryRemoteControlDeviceTree")
    public Rest<List<RemoteControlDeviceVO>> queryRemoteControlDeviceTree(@RequestBody RemoteControlDeviceQuery query){
        List<RemoteControlDeviceVO> remoteControlDeviceTreeList = scheduleTaskService.queryRemoteControlDeviceTree(query);
        return Rest.ok(remoteControlDeviceTreeList);
    }

}
